package com.imooc.framework.quartz;

import com.imooc.framework.quartz.MyJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class SchedulerExample {
    public static void main(String[] args) {
        try {
            // 创建SchedulerFactory实例
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();

            // 获取Scheduler实例
            Scheduler scheduler = schedulerFactory.getScheduler();

            // 定义一个任务
            JobDetail job = JobBuilder.newJob(MyJob.class)
                    .withIdentity("myJob", "group1")
                    .build();

            ScheduleBuilder<? extends Trigger> scheduleBuilder = null;
//            scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                    .withRepeatCount(0)
//                    .withIntervalInSeconds(1);

            scheduleBuilder = CronScheduleBuilder.cronSchedule("*/1 * * * * ?");

            // 定义一个触发器
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .withSchedule(scheduleBuilder)
                    .build();

            // 将任务和触发器关联到Scheduler
            scheduler.scheduleJob(job, trigger);
            System.out.println("任务开始执行......");
            // 启动Scheduler
            scheduler.start();

            // 关闭Scheduler
//            scheduler.shutdown();

//            // 参数
//            JobDataMap dataMap = new JobDataMap();
//            dataMap.put("name", "张三");
//            JobKey jobKey = JobKey.jobKey("myJob", "group1");
//            if (scheduler.checkExists(jobKey))
//            {
//                System.out.println("hhhhhh");
//                scheduler.triggerJob(jobKey, dataMap);
//            }

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }


}
